package com.zz.web.controller;

import com.zz.common.entity.ClientConfig;
import com.zz.common.entity.User;
import com.zz.common.entity.UserClient;
import com.zz.common.enums.ProxyType;
import com.zz.common.enums.Status;
import com.zz.web.common.R;
import com.zz.web.pojo.Menu;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/api")
public class ApiController {

    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("login")
    public R<User> login(@RequestBody User user) {
        System.out.println(user);
        return R.success(user);
    }

    /**
     * 获取所有客户端
     * @param userId
     * @return
     */
    @GetMapping("/getClientList/{userId}")
    public R<List<Menu>> getClientList(@PathVariable("userId") String userId) {
        UserClient userClient = new UserClient("1", "1", "Z+ Test", "123456", 6, false);
        Menu menu = new Menu();
        menu.setUserClient(userClient);
        menu.setId(userClient.getClientId());
        menu.setName(userClient.getClientName());
        System.out.println(menu);
        return R.success(Collections.singletonList(menu));
    }

    private static int num = 0;
    @GetMapping("getConfigList/{userId}")
    public R<List<ClientConfig>> getConfigList(@PathVariable("userId") String userId) {
        ClientConfig clientConfig = new ClientConfig("1", "1", ProxyType.HTTP,
                "www.baidu.com", "127.0.0.1", "1080", 6, num % 2 == 0 ? Status.OPEN : Status.CLOSE);
        return R.success(Collections.singletonList(clientConfig));
    }

    @PostMapping("/open/{configId}")
    public R open(@PathVariable("configId") String configId) {
        System.out.println(configId);
        num++;
        return R.success(null);
    }
}
